/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.action;

import java.util.ArrayList;
import java.util.Iterator;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;

/**
  * @version $Revision: 154 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class DeleteEmptyChangelistAction extends PerforceAction 
{
	// ------------------------------------------------------------------------------ Public Methods

	/** @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */
	public void run(IAction action) 
	{
		IChangelist[] changelists = getSelectedChangelists();
		Policy.assertTrue(changelists.length == 1);
		
		IChangelist changelist = changelists[0];
		if (confirmOperation(changelist))
		{
			IPerforceServer server = changelist.getServer();
			try
			{
				server.deleteChangelist(changelist);
			}
			catch (PerforceException e)
			{
				handle(e);
			}
			finally
			{
				IPerforceServer[] servers = { server };
				PerforceCorePlugin.getPlugin().broadcastChangelistStateChanges(servers);
			}
		}
	}
	
	// --------------------------------------------------------------------------- Protected Methods

	/** @see net.sourceforge.perforce.ui.action.PerforceAction#isEnabled() */
	protected boolean isEnabled() 
		throws PerforceException 
	{
		IChangelist[] changelists = getSelectedChangelists();
		if (changelists.length != 1)
		{
			return false;
		}
		
		IChangelist changelist = changelists[0];
		if (changelist.getId() == IChangelist.DEFAULT)
		{
			return false;
		}
		
		IP4File[] files = changelist.getFiles();
		if (files.length > 0)
		{
			return false;
		} 
		
		return true;
	}
	
	// ----------------------------------------------------------------------------- Private Methods
	
	/** Called to get the user's confirmation for the operation.
	  *
	  * @param changelist the changelist to delete.
	  * @return true if the user wants to continue, false otherwise. 
	  */
	private boolean confirmOperation(IChangelist changelist) 
	{
		String title = Policy.bind("DeleteEmptyChangelistAction.confirmationTitle");   //$NON-NLS-1$
		String message = 
			Policy.bind(
				"DeleteEmptyChangelistAction.confirmationMessage",                     //$NON-NLS-1$
				changelist.getId());
				
		return MessageDialog.openConfirm(getShell(), title, message);
	}
	
	/** Called to retrieve an array of selected changelists. 
	  * 
	  * @return array of selected changelists.
	  */
	private IChangelist[] getSelectedChangelists()
	{
		ArrayList resources = null;
		if (!getSelection().isEmpty()) 
		{
			resources = new ArrayList();
			for (Iterator elements = getSelection().iterator(); elements.hasNext();)
			{
				Object next = elements.next();
				if (next instanceof IChangelist) 
				{
					resources.add(next);
					continue;
				}
				else
				if (next instanceof IAdaptable) 
				{
					IAdaptable a = (IAdaptable) next;
					Object adapter = a.getAdapter(IChangelist.class);
					if (adapter instanceof IChangelist) 
					{
						resources.add(adapter);
						continue;
					}
				}
			}
		}
		
		if (resources != null && !resources.isEmpty()) 
		{
			return (IChangelist[]) resources.toArray(new IChangelist[resources.size()]);
		}
		
		return new IChangelist[0];
	}
}
